import Vue from 'vue'
import Router from 'vue-router'

// 路由懒加载
// import Login from '../components/Login.vue'
// import Home from '../components/Home.vue'
// import Welcome from '../components/Welcome.vue'
// import Users from '../components/User/Users.vue'
// import Roles from '../components/Authority/Roles.vue'
// import Rights from '../components/Authority/Rights.vue'
// import Goods from '../components/Goods/Goods.vue'
// import Add from '../components/Goods/Add.vue'
// import Params from '../components/Goods/Params.vue'
// import Categories from '../components/Goods/Categories.vue'
// import Orders from '../components/Orders/Orders.vue'
// import Reports from '../components/Reports/Reports.vue'
const Login = () => import(/* webpackChunkName: "Login" */ '../components/Login.vue')
const Home = () => import(/* webpackChunkName: "Home_Welcome" */ '../components/Home.vue')
const Welcome = () => import(/* webpackChunkName: "Home_Welcome" */ '../components/Welcome.vue')
const Users = () => import(/* webpackChunkName: "Users_Roles_Rights" */ '../components/User/Users.vue')
const Roles = () => import(/* webpackChunkName: "Users_Roles_Rights" */ '../components/Authority/Roles.vue')
const Rights = () => import(/* webpackChunkName: "Users_Roles_Rights" */ '../components/Authority/Rights.vue')
const Goods = () => import(/* webpackChunkName: "Goods_Add" */ '../components/Goods/Goods.vue')
const Add = () => import(/* webpackChunkName: "Goods_Add" */ '../components/Goods/Add.vue')
const Params = () => import(/* webpackChunkName: "Params_Categories" */ '../components/Goods/Params.vue')
const Categories = () => import(/* webpackChunkName: "Params_Categories" */ '../components/Goods/Categories.vue')
const Orders = () => import(/* webpackChunkName: "Orders" */ '../components/Orders/Orders.vue')
const Reports = () => import(/* webpackChunkName: "Reports" */ '../components/Reports/Reports.vue')

Vue.use(Router)

const router = new Router({
  //   mode: 'history',
  routes: [
    {
      path: '/login',
      component: Login
    },
    {
      path: '/',
      redirect: '/login'
    },
    {
      path: '/home',
      component: Home,
      redirect: '/welcome',
      children: [
        {
          path: '/welcome',
          component: Welcome
        },
        {
          path: '/users',
          component: Users
        },
        {
          path: '/roles',
          component: Roles
        },
        {
          path: '/rights',
          component: Rights
        },
        {
          path: '/goods',
          component: Goods
        },
        {
          path: '/goods/add',
          component: Add
        },
        {
          path: '/params',
          component: Params
        },
        {
          path: '/categories',
          component: Categories
        },
        {
          path: '/orders',
          component: Orders
        },
        {
          path: '/reports',
          component: Reports
        }
      ]
    }
  ]
})
router.beforeEach((to, _, next) => {
  // 添加访问权限校验
  // 访问的不是login，并且本地没有存储token，跳转到login
  if (to.path === '/login') return next()
  const tokenStr = window.sessionStorage.getItem('token')
  if (!tokenStr) return next('/login')
  next()
})
export default router
